package com.leyou.item.controller;

import com.leyou.item.dto.CategoryDTO;
import com.leyou.item.entity.Category;
import com.leyou.item.service.CategoryService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("category")
public class CategoryController {


    private final CategoryService categoryService;

    public CategoryController(CategoryService categoryService) {
        this.categoryService = categoryService;
    }

    /**
     * 根据id查询分类信息
     * @param id 分类id
     * @return 返回分类对象
     */
    @GetMapping("{id}")
    public ResponseEntity<CategoryDTO> queryCategoryById(@PathVariable("id") Long id){
        return ResponseEntity.ok(new CategoryDTO(categoryService.getById(id)));
    }

    /**
     * 根据id集合查询分类信息
     * @param ids 分类id集合
     * @return 返回分类集合对象
     */
    @GetMapping("list")
    public ResponseEntity<List<CategoryDTO>> queryCategoryByIds(@RequestParam("ids") List<Long> ids){
        List<Category> categories = categoryService.listByIds(ids);
        return ResponseEntity.ok( CategoryDTO.convertEntityList(categories));
    }

    /**
     * 根据父id查询分类集合
     * @param pid 父id
     * @return
     */
    @GetMapping("of/parent")
    public ResponseEntity<List<CategoryDTO>> queryCategoryByParentId(@RequestParam("pid") Long pid){
        List<Category> list = categoryService.query().eq("parent_id", pid).list();
        return ResponseEntity.ok(CategoryDTO.convertEntityList(list));
    }

    @GetMapping("of/brand")
    public  ResponseEntity<List<CategoryDTO>> queryCategoryByBrandId(@RequestParam("id") Long id){
           return ResponseEntity.ok(categoryService.queryCategoryByBrandId(id)) ;
    }
}
